package com.qujia.station.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.qujia.station.domain.GoToInvoice;
import com.qujia.station.domain.PayOrder;
import com.qujia.station.domain.vo.InvoiceInfoVo;
import com.qujia.station.mapper.InvoiceMapper;
import com.qujia.station.mapper.PayOrderMapper;
import com.qujia.station.service.InvoiceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Auther: zxy
 * @Date: 2023/8/8 12:35
 * @Description:
 */
@Service("InvoiceService")
@Transactional
public class InvoiceServiceImpl implements InvoiceService {

    @Resource
    private PayOrderMapper payOrderMapper;

    @Resource
    private InvoiceMapper invoiceMapper;

    @Override
    public int insertInvoiceInfo(GoToInvoice invoice, PayOrder payOrder) {
        //获取输入的开票金额
        Double invoiceAmount = invoice.getInvoiceAmount();
        BigDecimal invoicebigDecimal = new BigDecimal(String.valueOf(invoiceAmount));
        //订单支付金额
        BigDecimal amtbigDecimal = payOrder.getAmt();
        //开票金额不能大于支付金额
        if (invoicebigDecimal.compareTo(amtbigDecimal) < 0) {
            //剩余可开票金额
            BigDecimal balance = amtbigDecimal.subtract(invoicebigDecimal);
            InvoiceInfoVo invoiceInfo = InvoiceInfoVo.builder()
                    .invoiceAmount(invoiceAmount)
                    .orderNo(invoice.getOrderNo())
                    .invoiceStatus(2)
                    .invoiceBalance(balance)
                    .build();
            invoiceMapper.insertInvoice(invoiceInfo);
            InvoiceInfoVo invoiceInfoVo = invoiceMapper.selectInvoiceInfoByOrdNo(invoice.getOrderNo());
            //插入开票记录
            InvoiceInfoVo invoiceRecord = InvoiceInfoVo.builder()
                    .invoiceId(invoiceInfoVo.getInvoiceId())
                    .invoiceAmount(invoiceAmount)
                    .orderNo(invoice.getOrderNo())
                    .invoiceStatus(invoiceInfoVo.getInvoiceStatus())
                    .invoiceBalance(invoiceInfoVo.getInvoiceBalance())
                    .build();
            return invoiceMapper.insertInvoiceRecord(invoiceRecord);

        } else if (invoicebigDecimal.compareTo(amtbigDecimal) == 0) {
            InvoiceInfoVo invoiceInfo = InvoiceInfoVo.builder()
                    .invoiceAmount(invoiceAmount)
                    .orderNo(invoice.getOrderNo())
                    .invoiceBalance(BigDecimal.valueOf(0.00))
                    .invoiceStatus(1)
                    .build();
            invoiceMapper.insertInvoice(invoiceInfo);
            InvoiceInfoVo invoiceInfoVo = invoiceMapper.selectInvoiceInfoByOrdNo(invoice.getOrderNo());
            //插入开票记录
            InvoiceInfoVo invoiceRecord = InvoiceInfoVo.builder()
                    .invoiceId(invoiceInfoVo.getInvoiceId())
                    .invoiceAmount(invoiceAmount)
                    .orderNo(invoice.getOrderNo())
                    .invoiceStatus(invoiceInfoVo.getInvoiceStatus())
                    .invoiceBalance(invoiceInfoVo.getInvoiceBalance())
                    .build();
            return invoiceMapper.insertInvoiceRecord(invoiceRecord);
        }
        return 0;
    }

    @Override
    public InvoiceInfoVo selectInvoiceInfoByOrdNo(String orderNo) {
        InvoiceInfoVo invoiceInfoVo = invoiceMapper.selectInvoiceInfoByOrdNo(orderNo);
        if (ObjectUtils.isEmpty(invoiceInfoVo)){
            PayOrder payOrder = payOrderMapper.selectOrderInfoByOrdNo(orderNo);
            if (ObjectUtils.isEmpty(payOrder)){
                try {
                    throw new Exception("没找到该订单信息");
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
            return InvoiceInfoVo.builder()
                    .invoiceBalance(payOrder.getAmt())
                    .orderNo(payOrder.getOrderNo())
                    .invoiceStatus(0)
                    .build();
        }
        return invoiceInfoVo;
    }

    @Override
    @Transactional
    public int updateInvoiceInfo(InvoiceInfoVo invoiceInfo, GoToInvoice invoice) {
        //旧的开票金额
        BigDecimal oldAmount = convertUtil(invoiceInfo.getInvoiceAmount());
        //新的开票金额
        BigDecimal amount = convertUtil(invoice.getInvoiceAmount());
        //相加
        BigDecimal addInvoiceAmount = oldAmount.add(amount);
        InvoiceInfoVo invoiceInfoVo = new InvoiceInfoVo();
        BeanUtil.copyProperties(invoiceInfo, invoiceInfoVo);
        invoiceInfoVo.setInvoiceAmount(addInvoiceAmount.doubleValue());
        //剩余可开票金额
        BigDecimal balance = invoiceInfo.getInvoiceBalance();
        BigDecimal subtract = balance.subtract(amount);
        invoiceInfoVo.setInvoiceBalance(subtract);
        //判断更改状态
        if (subtract.compareTo(BigDecimal.ZERO) == 0) {
            invoiceInfoVo.setInvoiceStatus(1);
        }
        invoiceMapper.updateInvoiceInfo(invoiceInfoVo);
        //插入开票记录
        InvoiceInfoVo invoiceRecord = InvoiceInfoVo.builder()
                .invoiceId(invoiceInfoVo.getInvoiceId())
                .invoiceAmount(invoice.getInvoiceAmount())
                .orderNo(invoice.getOrderNo())
                .invoiceStatus(invoiceInfoVo.getInvoiceStatus())
                .invoiceBalance(subtract)
                .build();
        return invoiceMapper.insertInvoiceRecord(invoiceRecord);
    }

    @Override
    public List<InvoiceInfoVo> getInvoiceRecords(String orderNo,String businessName) {
        return invoiceMapper.selectInvoiceRecordsByOrdNoAndBusinessName(orderNo,businessName);
    }

    private BigDecimal convertUtil(Double d) {
        return new BigDecimal(String.valueOf(d));
    }
}
